package cc.rengu.redp.common.render.core;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * springboot启动时自动注册继承自BaseDelegate的类
 */
@Service
public class RegAllRenderers implements ApplicationListener<ContextRefreshedEvent> {

    @Autowired
    private RenderService renderService;

    @Override
    public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
        //获取Spring容器
        ApplicationContext applicationContext = contextRefreshedEvent.getApplicationContext();
        //获取全部Renderer的衍生类
        Map<String, Renderer> beans = applicationContext.getBeansOfType(Renderer.class);

        for (Map.Entry<String, Renderer> en : beans.entrySet()) {
            renderService.addRender(en.getValue().getClass());
        }
    }
}
